package com.hcl.controller;

import com.hcl.dao.BookDao;
import com.hcl.entity.Book;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * 图书控制器
 */
@Controller
@RequestMapping("/book")       //映射
public class BookController {

    @Resource
    private BookDao bookDao;

    /**
     * 查询所有图书
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list(){
        ModelAndView mav=new ModelAndView();
        mav.addObject("bookList", bookDao.findAll());
        mav.setViewName("bookList");
        return mav;
    }

    /**
     * 根据条件动态查询
     * @param book
     * @return
     */
    @RequestMapping("/list2")
    public ModelAndView list2(Book book){
        ModelAndView mav=new ModelAndView();
        //Specification有一个实现toPredicate
        List<Book> bookList=bookDao.findAll(new Specification<Book>() {
            @Override
            //CriteriaQuery<?> criteriaQuery是一种比较高级的查询，一般不用；
            //一般不用criteriaQuery
            public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                Predicate predicate=cb.conjunction();
                //动态拼接条件
                if(book!=null){
                    if(book.getName()!=null && !"".equals(book.getName())){
                        predicate.getExpressions().add(cb.like(root.get("name"),"%"+book.getName()+"%"));
                    }
                    if(book.getAuthor()!=null && !"".equals(book.getAuthor())){
                        predicate.getExpressions().add(cb.like(root.get("author"),"%"+book.getAuthor()+"%"));
                    }
                }
                return predicate;
            }
        });
        mav.addObject("bookList",bookList);
        mav.setViewName("bookList");
        return mav;
    }

    /**
     * 添加操作
     * @param book
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    //这个意思是它的映射是/add，然后是只能以POST方法发送；
    public String add(Book book){
        bookDao.saveAndFlush(book);
        return "forward:/book/list";
    }

    /**
     * 根据id来查找book实体
     * @param id
     * @return
     */
    @RequestMapping("/PreUpdate/{id}")
    public ModelAndView PreUpdate(@PathVariable("id") Integer id){
        ModelAndView mav=new ModelAndView();
        mav.addObject("book",bookDao.getOne(id));
        mav.setViewName("bookUpdate");
        return mav;
    }

    /**
     * 修改图书
     * @param book
     * @return
     */
    @PostMapping(value = "/update") //这里的意思其实和添加操作是一样的
    public String Update(Book book){
        bookDao.save(book);
        return "forward:/book/list";
    }

    /**
     * 进行删除操作
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public String delete(Integer id){
        bookDao.delete(id);
        return "forward:/book/list";
    }

    @ResponseBody               //因为返回的是ajax，所以要加上这个
    @GetMapping("/queryByName")
    public List<Book> queryByName(){
        return bookDao.findByName("6");
    }

    //就是相当于是随机从数据库中选2条
    @ResponseBody
    @GetMapping("/randomList")
    public List<Book> randomList(){
        return bookDao.randomList(2);
    }
}
